package com.eeepay.eeepay_v2.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.util.SpanUtils;
import com.eeepay.v2_library.view.LabelEditText;
import com.eeepay.v2_library.view.LeftRightText;

import java.util.HashMap;

import cn.eeepay.api.grpc.nano.MerAddModular;

/**
 * Created by Ching on 2016/4/13.
 */
public class RateAdapter extends BaseAdapter {

    private MerAddModular.ServiceRate[] rateList;
    private Context mContext;
    //定义hashMap 用来存放之前创建的每一项item
    HashMap<Integer, View> lmap = new HashMap<Integer, View>();

    public RateAdapter(Context mContext) {
        this.mContext = mContext;

    }

    public void addAll(MerAddModular.ServiceRate[] rateList) {
        this.rateList = rateList;
        notifyDataSetChanged();
    }

    public MerAddModular.ServiceRate[] getDatas() {
        return rateList;
    }

    @Override
    public int getCount() {
        return rateList.length;
    }

    @Override
    public Object getItem(int position) {
        return rateList[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private Integer index = -1;

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        final MerAddModular.ServiceRate rateInfo = rateList[position];
        if (lmap.get(position) == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_service_rate_list, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
            lmap.put(position, convertView);
        } else {
            convertView = lmap.get(position);
            holder = (ViewHolder) convertView.getTag();
        }

//        if (convertView != null) {
//            holder = (ViewHolder) convertView.getTag();
//        } else {
//            holder = new ViewHolder(convertView);
//            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_service_rate_list, null);
//
//            convertView.setTag(holder);
//        }
        Log.d("saki", " rateType " + rateInfo.rateType);
        switch (Integer.parseInt(rateInfo.rateType)) {
            case 1:
                holder.amount1.setVisibility(View.VISIBLE);
                holder.rate2Ll.setVisibility(View.GONE);
                holder.rate3Ll.setVisibility(View.GONE);
                holder.rate4Ll.setVisibility(View.GONE);
                holder.rate5Rl.setVisibility(View.GONE);
                holder.amount1.setText(rateInfo.singleNumAmount);
                holder.amount1.setTag(position);
                if ("1".equals((rateInfo.fixedRate))) {//费率不可更改
                    Log.d("saki", " fixedRate " + rateInfo.fixedRate);
                    holder.amount1.setEnabled(false);
                    holder.amount1.setBackgroundColor(Color.WHITE);
                    holder.amount1.setPadding(0, 0, 0, 0);
                } else {
                    class Rate1Listener implements TextWatcher {
                        EditText editText;

                        public Rate1Listener(EditText editText) {
                            this.editText = editText;
                        }

                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                        }

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {
                        }

                        @Override
                        public void afterTextChanged(Editable s) {
                            rateList[position].rate = s.toString();
                        }
                    }
                    holder.amount1.addTextChangedListener(new Rate1Listener(holder.amount1));
                }
                break;
            case 2:
                holder.rate2Ll.setVisibility(View.VISIBLE);
                holder.amount1.setVisibility(View.GONE);
                holder.rate3Ll.setVisibility(View.GONE);
                holder.rate4Ll.setVisibility(View.GONE);
                holder.rate5Rl.setVisibility(View.GONE);
                holder.rate2Et.setText(rateList[position].rate);

                holder.rate2Et.setTag(position);
                if ("1".equals((rateInfo.fixedRate))) {//费率不可更改
                    holder.rate2Et.setEnabled(false);
                    holder.rate2Et.setBackgroundColor(Color.WHITE);
                    holder.rate2Et.setPadding(0, 0, 0, 0);
                } else {
                    holder.rate2Et.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            if (event.getAction() == MotionEvent.ACTION_UP) {
                                index = (Integer) v.getTag();
                            }
                            return false;
                        }
                    });
                    class Rate2Listener implements TextWatcher {
                        EditText editText;
                        public Rate2Listener(EditText editText) {
                            this.editText = editText;
                        }
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                        }
                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {
                        }
                        @Override
                        public void afterTextChanged(Editable s) {
                            rateList[position].rate = s.toString();
                        }
                    }
                    holder.rate2Et.addTextChangedListener(new Rate2Listener(holder.rate2Et));
                }
                break;
            case 3:
                holder.rate3Ll.setVisibility(View.VISIBLE);
                holder.amount1.setVisibility(View.GONE);
                holder.rate2Ll.setVisibility(View.GONE);
                holder.rate4Ll.setVisibility(View.GONE);
                holder.rate5Rl.setVisibility(View.GONE);

                holder.rate3Et.setText(rateInfo.rate);
                holder.safeLine.setText(rateInfo.safeLine);
                holder.capping.setText(rateInfo.capping);
                holder.rate3Et.setTag(position);
                if ("1".equals(rateInfo.fixedRate)) {
                    holder.rate3Et.setEnabled(false);
                    holder.safeLine.setEnabled(false);
                    holder.capping.setEnabled(false);
                    holder.rate3Et.setBackgroundColor(Color.WHITE);
                    holder.safeLine.setBackgroundColor(Color.WHITE);
                    holder.capping.setBackgroundColor(Color.WHITE);
                } else {
                    class Rate3Listener implements TextWatcher {
                        EditText editText;

                        public Rate3Listener(EditText editText) {
                            this.editText = editText;
                        }

                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                        }

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {
                        }

                        @Override
                        public void afterTextChanged(Editable s) {
//                            int position = (int) holder.rate3Et.getTag();
//                            int position = (int) editText.getTag();
                            switch (editText.getId()) {
                                case R.id.et_safeLine:
                                    Log.d("afterTextChanged", "safeLine");
                                    rateList[position].safeLine = s.toString();
                                    break;
                                case R.id.et_rate3:
                                    Log.d("afterTextChanged", "rate3");
                                    rateList[position].rate = s.toString();
                                    break;
                                case R.id.et_capping:
                                    Log.d("afterTextChanged", "capping");
                                    rateList[position].capping = s.toString();
                                    break;
                            }

                        }
                    }
                    holder.rate3Et.addTextChangedListener(new Rate3Listener(holder.rate3Et));
                    holder.safeLine.addTextChangedListener(new Rate3Listener(holder.safeLine));
                    holder.capping.addTextChangedListener(new Rate3Listener(holder.capping));
                }
                break;
            case 4:
                holder.rate4Ll.setVisibility(View.VISIBLE);
                holder.amount1.setVisibility(View.GONE);
                holder.rate2Ll.setVisibility(View.GONE);
                holder.rate3Ll.setVisibility(View.GONE);
                holder.rate5Rl.setVisibility(View.GONE);

                holder.rate4Et.setText(rateInfo.rate);
                holder.amount4.setText(rateInfo.singleNumAmount);
                holder.rate4Et.setTag(position);
                if ("1".equals(rateInfo.fixedRate)) {
                    holder.rate4Et.setEnabled(false);
                    holder.rate4Et.setBackgroundColor(Color.WHITE);
                } else {
                    class Rate4Listener implements TextWatcher {
                        EditText editText;

                        public Rate4Listener(EditText editText) {
                            this.editText = editText;
                        }

                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                        }

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {
                        }

                        @Override
                        public void afterTextChanged(Editable s) {
                            int position = (int) editText.getTag();
                            rateList[position].rate = s.toString();
                        }
                    }
                    holder.rate4Et.addTextChangedListener(new Rate4Listener(holder.rate4Et));
                }
                break;
            case 5:
                holder.rate5Rl.setVisibility(View.VISIBLE);
                holder.amount1.setVisibility(View.GONE);
                holder.rate2Ll.setVisibility(View.GONE);
                holder.rate3Ll.setVisibility(View.GONE);
                holder.rate4Ll.setVisibility(View.GONE);

                holder.rate5Ldr1Et.setText(rateInfo.ladder1Rate);
                holder.rate5Ldr2Et.setText(rateInfo.ladder2Rate);
                holder.ldr1Max.setText(rateInfo.ladder1Max);
                holder.rate5Ldr1Et.setTag(position);
                holder.rate5Ldr2Et.setTag(position);

                if ("1".equals(rateInfo.fixedRate)) {
                    holder.rate5Ldr1Et.setEnabled(false);
                    holder.rate5Ldr2Et.setEnabled(false);
                    holder.rate5Ldr1Et.setBackgroundColor(Color.WHITE);
                    holder.rate5Ldr2Et.setBackgroundColor(Color.WHITE);
                } else {
                    class Rate5Listener implements TextWatcher {
                        EditText editText;

                        public Rate5Listener(EditText editText) {
                            this.editText = editText;
                        }

                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                        }

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {
                        }

                        @Override
                        public void afterTextChanged(Editable s) {
                            switch (editText.getId()) {
                                case R.id.et_ldr1_rate:
                                    int position1 = (int) editText.getTag();
                                    rateList[position1].ladder1Rate = s.toString();
                                    break;
                                case R.id.et_ldr2_rate:
                                    int position2 = (int) editText.getTag();
                                    rateList[position2].ladder2Rate = s.toString();
                                    break;
                            }

                        }
                    }
                    holder.rate5Ldr1Et.addTextChangedListener(new Rate5Listener(holder.rate5Ldr1Et));
                    holder.rate5Ldr2Et.addTextChangedListener(new Rate5Listener(holder.rate5Ldr2Et));
//                    class Rate5Ldr2Listener implements TextWatcher {
//                        EditText editText;
//
//                        public Rate5Ldr2Listener(EditText editText) {
//                            this.editText = editText;
//                        }
//
//                        @Override
//                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//                        }
//
//                        @Override
//                        public void onTextChanged(CharSequence s, int start, int before, int count) {
//                        }
//
//                        @Override
//                        public void afterTextChanged(Editable s) {
////                            int position = (int) holder.rate5Ldr2Et.getTag();
//
//                        }
//                    }

                }
                break;
        }

        holder.serName.setRightText(rateInfo.serviceName);
        int type = Integer.parseInt(rateInfo.cardType);
        String cardType = null;
        switch (type) {
            case 0:
                cardType = "不限";
                break;
            case 1:
                cardType = "信用卡";
                break;
            case 2:
                cardType = "储蓄卡";
                break;
        }
        holder.cardType.setRightText(cardType);
        int time = Integer.parseInt(rateInfo.holidaysMark);
        String serviceTime = null;
        switch (time) {
            case 0:
                serviceTime = "不限";
                break;
            case 1:
                serviceTime = "工作日";
                break;
            case 2:
                serviceTime = "节假日";
                break;
        }
        holder.serTime.setRightText(serviceTime);
        holder.rate2Et.clearFocus();
        if (index != -1 && index == position) {
            holder.rate2Et.requestFocus();
        }
        return convertView;
    }

    class ViewHolder {
        public LeftRightText serName, cardType, serTime, type;
        public EditText amount1, rate2Et, rate3Et, rate4Et, rate5Ldr1Et, rate5Ldr2Et, safeLine, capping;
        public TextView amount4, ldr1Max;
        public RelativeLayout rate2Ll, rate4Ll, rate5Rl;
        public LinearLayout rate3Ll;

        public ViewHolder(View convertView) {
            serName = (LeftRightText) convertView.findViewById(R.id.lrt_service_name);
            cardType = (LeftRightText) convertView.findViewById(R.id.lrt_service_card_type);
            serTime = (LeftRightText) convertView.findViewById(R.id.lrt_service_time);

//            amount1 = (TextView) convertView.findViewById(R.id.et_amount1);
            rate2Ll = (RelativeLayout) convertView.findViewById(R.id.ll_service_rate2);
            rate3Ll = (LinearLayout) convertView.findViewById(R.id.ll_rate3);
            rate4Ll = (RelativeLayout) convertView.findViewById(R.id.ll_rate4);
            rate5Rl = (RelativeLayout) convertView.findViewById(R.id.rl_rate5);

            amount1 = (EditText) convertView.findViewById(R.id.et_amount1);

            rate2Et = (EditText) convertView.findViewById(R.id.et_rate2);

            rate3Et = (EditText) convertView.findViewById(R.id.et_rate3);
            safeLine = (EditText) convertView.findViewById(R.id.et_safeLine);
            capping = (EditText) convertView.findViewById(R.id.et_capping);

            rate4Et = (EditText) convertView.findViewById(R.id.et_rate4);
            amount4 = (TextView) convertView.findViewById(R.id.tv_amount4);

            rate5Ldr1Et = (EditText) convertView.findViewById(R.id.et_ldr1_rate);
            rate5Ldr2Et = (EditText) convertView.findViewById(R.id.et_ldr2_rate);
            ldr1Max = (TextView) convertView.findViewById(R.id.tv_ldr1_max);
        }
    }


}
